package com.yj.blog.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.yj.blog.admin.service.SysResourceService;
import com.yj.blog.admin.service.SysUserCacheService;
import com.yj.blog.mapper.SysResourceMapper;
import com.yj.blog.model.SysResource;
import com.yj.blog.model.SysResourceExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 资源管理Service实现类
 */
@Service
public class SysResourceServiceImpl implements SysResourceService {
    @Autowired
    private SysResourceMapper resourceMapper;
    @Autowired
    private SysUserCacheService userCacheService;
    @Override
    public int create(SysResource sysResource) {
        sysResource.setCreateTime(new Date());
        return resourceMapper.insert(sysResource);
    }

    @Override
    public int update(Long id, SysResource sysResource) {
        sysResource.setId(id);
        int count = resourceMapper.updateByPrimaryKeySelective(sysResource);
        userCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public SysResource getItem(Long id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        int count = resourceMapper.deleteByPrimaryKey(id);
        userCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public List<SysResource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SysResourceExample example = new SysResourceExample();
        SysResourceExample.Criteria criteria = example.createCriteria();
        if(categoryId!=null){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        if(StrUtil.isNotEmpty(nameKeyword)){
            criteria.andNameLike('%'+nameKeyword+'%');
        }
        if(StrUtil.isNotEmpty(urlKeyword)){
            criteria.andUrlLike('%'+urlKeyword+'%');
        }
        return resourceMapper.selectByExample(example);
    }

    @Override
    public List<SysResource> listAll() {
        return resourceMapper.selectByExample(new SysResourceExample());
    }
}
